{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Launch the Kubeflow Dashboard\n",
    "This deploys `istio-ingress` in the `istio-system` namespace and creates a publicly-available `LoadBalancer` endpoint.\n",
    "\n",
    "# _Note:  THIS IS A PUBLIC ENDPOINT!!_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    ". ~/.bash_profile\n",
    "\n",
    "############################\n",
    "# THIS IS A PUBLIC ENDPOINT\n",
    "############################\n",
    "cd ${KF_DIR}/.cache/manifests/manifests-1.0.2/\n",
    "kubectl apply -k aws/istio-ingress/base --namespace istio-system\n",
    "cd ${KF_DIR}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run the Next Cell Until You See a Valid URL \n",
    "\n",
    "Notes:\n",
    "* If you see an empty `http://` endpoint, then you need to uncomment out the code above, but be careful because the endpoint is public and you may be hacked!\n",
    "\n",
    "* The endpoint will look something like this:  `http://[some-long-subdomain-name].[aws-region].elb.amazonaws.com`\n",
    "\n",
    "* Navigate to the Kubeflow Dashboard at this URL.  THIS WILL TAKE A FEW MINUTES AS DNS IS EVENTUALLY CONSISTENT AND TAKES A FEW MINUTES TO PROPAGATE ACROSS THE WORLD.  \n",
    "\n",
    "* If you see a 404 in your browser, please be patient.  This will take a few minutes as mentioned above.\n",
    "\n",
    "# _READ ^^ ABOVE ^^ BEFORE MOVING FORWARD._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "sleep 10\n",
    "\n",
    "echo \" THIS LINK MAY TAKE A FEW MINUTES TO SHOW UP.  PATIENCE \"\n",
    "echo \"\"\n",
    "echo \"********************************************************\"\n",
    "echo \"        CLICK THE FOLLOWING LINK WHEN IT APPEARS        \"\n",
    "echo \"\"\n",
    "echo http://$(kubectl get ingress -n istio-system -o jsonpath='{.items[0].status.loadBalancer.ingress[0].hostname}')\n",
    "echo \"\"\n",
    "echo \"^^^^^^ COPY/PASTE THIS URL INTO A NEW BROWSER TAB ^^^^^^\"\n",
    "echo \"********************************************************\"\n",
    "echo \"\"\n",
    "echo \"====> FOLLOW INSTRUCTIONS BELOW IN NEW BROWSER TAB <====\"\n",
    "echo \"\"\n",
    "echo \"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# _RE-RUN ^^ ABOVE ^^ IF YOU DON'T SEE A VALID `http://...` URL_\n",
    "# _AFTER YOU CLICK THE VALID `http://...` URL, IT MAY TAKE A FEW MINUTES TO PROPAGATE THROUGH DNS .  PLEASE BE PATIENT._\n",
    "\n",
    "# _FOLLOW THE STEPS BELOW AFTER YOU SEE THE Kubeflow Dashboard._\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Kubeflow Anonymous Namespace](img/kubeflow-anonymous-namespace.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Click on `Start Setup`.\n",
    "\n",
    "**Note:  You must use the default namespace `anonymous`.**\n",
    "\n",
    "Click `Finish` to view the dashboard."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# You should continue when you see the following Kubeflow Dashboard."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Kubeflow Dashboard](img/kubeflow-dashboard.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Launch Kubeflow Notebook Server\n",
    "\n",
    "Kubeflow Notebooks are based on Jupyter Notebooks.  They are open-source web applications that allow you to create and share documents that contain live code, equations, visualizations and narrative text.  They are often used for data cleaning, transformations, analysis, and visualizations.  Additionally, Kubeflow and Jupyter Notebooks are used for numerical simulations, statistical modeling, machine learning, and artificial intelligence."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a New Kubeflow Notebook Server\n",
    "![dashboard](img/dashboard-new-notebook-server.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Select the `anonymous` Namespace\n",
    "\n",
    "![dashboard](img/jupyter-select-namespace.png)\n",
    "\n",
    "This pre-populates the namespace field on the dashboard. Specify a name **notebook** for the notebook:\n",
    "\n",
    "![dashboard](img/jupyter-enter-notebook-server-name.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check the `Custom Image` Checkbox \n",
    "\n",
    "# Specify Our Optimized Notebook Image:\n",
    "\n",
    "*********************************************\n",
    "```\n",
    "pipelineai/kubeflow-notebook-cpu-1.13.1:2.0.0\n",
    "\n",
    "```\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "*********************************************\n",
    "\n",
    "![Select Image](img/jupyter-select-image.png)\n",
    "\n",
    "*********************************************\n",
    "```\n",
    "pipelineai/kubeflow-notebook-cpu-1.13.1:2.0.0\n",
    "\n",
    "```\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "*********************************************\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Change the CPU to **1.0** (Not More, Not Less)\n",
    "\n",
    "![Select CPU](img/jupyter-select-cpu.png)\n",
    "\n",
    "Scroll to the bottom, accept all other defaults, and click on **LAUNCH**.\n",
    "\n",
    "![Launch Notebook Server](img/kubeflow-notebook-server-launch.png)\n",
    "\n",
    "It takes a couple minutes for the first Jupyter notebook to come online. Click on **CONNECT**\n",
    "\n",
    "![List Notebook Servers](img/jupyter-notebook-servers.png)\n",
    "\n",
    "This connects to the notebook and opens the notebook interface in a new browser tab.\n",
    "\n",
    "![Connect](img/jupyter-new-notebook.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Launch a New Terminal in the Notebook\n",
    "\n",
    "Click on **New**, select **Terminal**\n",
    "\n",
    "![New Terminal](img/jupyter-new-terminal.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Clone our Repo in the Terminal\n",
    "\n",
    "*********************************************\n",
    "```\n",
    "git clone https://github.com/data-science-on-aws/workshop\n",
    "\n",
    "```\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "*********************************************\n",
    "\n",
    "![](img/clone-repo-kubeflow-notebook.png)\n",
    "\n",
    "\n",
    "*********************************************\n",
    "```\n",
    "git clone https://github.com/data-science-on-aws/workshop\n",
    "\n",
    "```\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "*********************************************"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run the Kubeflow Notebooks\n",
    "\n",
    "**Note:** Make sure you are in the Kubeflow Jupyter Notebook (not SageMaker Jupyter Notebook.)\n",
    "\n",
    "Navigate to the `workshop/10_pipeline/kubeflow/` directory and start running the notebooks from `01_*`.\n",
    "\n",
    "![run-notebooks](img/run-notebooks.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %%html\n",
    "\n",
    "# <p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "# <button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "\n",
    "# <script>\n",
    "# try {\n",
    "#     els = document.getElementsByClassName(\"sm-command-button\");\n",
    "#     els[0].click();\n",
    "# }\n",
    "# catch(err) {\n",
    "#     // NoOp\n",
    "# }\n",
    "# </script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %%javascript\n",
    "\n",
    "# try {\n",
    "#     Jupyter.notebook.save_checkpoint();\n",
    "#     Jupyter.notebook.session.delete();\n",
    "# }\n",
    "# catch(err) {\n",
    "#     // NoOp\n",
    "# }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
